package com.dmjj.limit.controller;

import com.alibaba.fastjson.JSONArray;
import com.dmjj.common.base.BaseController;
import com.dmjj.common.base.LoginEntity;
import com.dmjj.common.result.ResultEnum;
import com.dmjj.common.result.ResultObject;
import com.dmjj.common.util.ListCopyToVOUtil;
import com.dmjj.common.util.PaginationVOUtil;
import com.dmjj.common.vo.ResultVO;
import com.dmjj.limit.dto.RoleLimitDTO;
import com.dmjj.limit.service.RoleLimitService;
import com.dmjj.limit.vo.RoleLimitSaveVO;
import com.dmjj.limit.vo.RoleLimitSaveVO2;
import com.dmjj.limit.vo.RoleLimitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description 角色-资源表实体类控制器
 * @Author zhangxue
 * @Date 2019-03-21 09:40:56
 */

@RestController
@Api(tags = "角色-资源表实体类控制器")
@RequestMapping("roleLimit")
@Transactional
public class RoleLimitController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(RoleLimitController.class);

    @Autowired
    private RoleLimitService service;
    @Autowired
    private ResultObject resultObject;

    /**
	 * 角色-资源表实体类列表
     * @Author zhangxue
     * @Date 2019-03-21 09:40:56
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "角色-资源表实体类列表", response = RoleLimitVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            RoleLimitVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            RoleLimitDTO entityDTO = new RoleLimitDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<RoleLimitDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, RoleLimitVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 角色-资源表实体类列表（无分页）
     * @Author zhangxue
     * @Date 2019-03-21 09:40:56
     * @Param
     * @Return
     */
    @ApiOperation(value = "角色-资源表实体类列表（无分页）", response = RoleLimitVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(RoleLimitVO entity){
        try {
            RoleLimitDTO entityDTO = new RoleLimitDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<RoleLimitDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, RoleLimitVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 角色-资源表实体类详细信息
     * @Author zhangxue
     * @Date 2019-03-21 09:40:56
     * @Param
     * @Return
     */
    @ApiOperation(value = "角色-资源表实体类详细信息", response = RoleLimitVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            RoleLimitDTO entityDTO = service.queryOneByPk(id);
            RoleLimitVO entity = new RoleLimitVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存角色-资源表实体类信息
     * @Author zhangxue
     * @Date 2019-03-21 09:40:56
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存角色-资源表实体类信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(RoleLimitSaveVO entity) {
        try {
//            LoginEntity loginEntity = getUserInfo();
            String Rolelimitjson = entity.getRolelimitjson();
            //saveList是要存入数据库的数据
            List<RoleLimitDTO> saveList = new ArrayList<RoleLimitDTO>();
            //获取所有需要插入的资源Id和其父节点Id
            List<RoleLimitSaveVO2> ts = JSONArray.parseArray(Rolelimitjson,RoleLimitSaveVO2.class);
            for(int i=0;i<ts.size();i++){
                RoleLimitVO tempRoleLim = new RoleLimitVO();
                tempRoleLim.setLimitid(ts.get(i).getLimitid());
                tempRoleLim.setLimparentid(ts.get(i).getLimparentid());
                tempRoleLim.setRoleid(entity.getRoleid());
                tempRoleLim.setCreatetime(new Date());
//                tempRoleLim.setCreatemanno(loginEntity.getUserid());
//                tempRoleLim.setCreatemanname(loginEntity.getName());
                RoleLimitDTO entityDTO = new RoleLimitDTO();
                BeanUtils.copyProperties(tempRoleLim, entityDTO);
                saveList.add(entityDTO);
            }
            Integer i = service.saveList(saveList);
            if(i > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除角色-资源表实体类信息
     * @Author zhangxue
     * @Date 2019-03-21 09:40:56
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除角色-资源表实体类信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}